home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / network / pktdrvtp.zip / PKTDRVR.ZIP / PKTDRVR.TXT < prev   
Text File  |  1993-10-08  |  12KB  |  348 lines

  1.  
  2.                     PKTDRVR Interface for Turbo Pascal 7.0
  3.                                   Version 1.0
  4.                         Written 1993 by Oliver Rehmann
  5.                           (released to public domain)
  6.  
  7.  
  8. What is PKTDRVR ?
  9. =================
  10.  
  11. PKTDRVR is a unit for Turbo Pascal 7.0. It provides an object oriented
  12. interface for crynrware packet drivers.
  13.  
  14. The Object
  15. ==========
  16.  
  17.        TPKTDRVR = OBJECT(TOBJECT)
  18.  
  19. private           { member variables }
  20.                   pktInt         : Integer;
  21.                   pktHandle      : Integer;
  22.                   pktRecvHandler : Pointer;
  23.                   pktStatus      : TPKTSTATUS;
  24.                   pktError       : Byte;
  25.                   pktRegs        : Registers;
  26.  
  27.                   pktAccessInfo  : TACCESSTYPE;
  28.  
  29.                   PROCEDURE   TestForPktDriver;
  30.  
  31. public            { member functions }
  32.  
  33.                   CONSTRUCTOR Init(IntNo : Integer);
  34.                   DESTRUCTOR  Done; VIRTUAL;
  35.  
  36.                   PROCEDURE   ScanForPktDriver;
  37.                   FUNCTION    GetStatus                          : TPKTSTATUS;
  38.                   FUNCTION    GetError                           : Byte;
  39.                   FUNCTION    GetHandle                          : Word;
  40.  
  41.                   PROCEDURE   GetAccessType   (VAR pktAccessType : TACCESSTYPE);
  42.                   PROCEDURE   DriverInfo      (VAR pktInfo       : TDRVRINFO  );
  43.  
  44.                   PROCEDURE   AccessType      (VAR pktAccessType : TACCESSTYPE);
  45.                   PROCEDURE   ReleaseType;
  46.                   PROCEDURE   TerminateDriver;
  47.  
  48.                   PROCEDURE   GetAddress      (Buffer : Pointer;BufLen : Word; VAR BufCopied : Word);
  49.                   PROCEDURE   ResetInterface;
  50.                   PROCEDURE   GetParameters   (VAR pktParams : TPKTPARAMS);
  51.  
  52.                   PROCEDURE   SendPkt         (Buffer : Pointer;BufLen : Word );
  53.                   PROCEDURE   As_SendPkt      (Buffer : Pointer;BufLen : Word;Upcall : Pointer     );
  54.  
  55.                   PROCEDURE   SetRCVmode      (Mode   : Word);
  56.                   FUNCTION    GetRCVmode              : Word;
  57.  
  58.                   PROCEDURE   SetMulticastList(VAR mcList : Pointer; VAR mcLen : Word);
  59.                   PROCEDURE   GetMulticastList(VAR mcList : Pointer; VAR mcLen : Word);
  60.  
  61.                   PROCEDURE   GetStatistics   (VAR pktStatistics : TSTATISTICS       );
  62.                   PROCEDURE   SetAddress      (Address : Pointer; VAR AddrLen  : Word);
  63.  
  64.                   END;
  65.  
  66. The object provides all functions found in the packet driver specification
  67. 1.09 from FTP. The functions will not be explained here.
  68. If more information is needed, consult the file PACKET_D.109 delivered with 
  69. Crynrware packet drivers.
  70.  
  71. The most important advantage is that you don't have to pass the handle and
  72. the packet driver interrupt to the different functions. These variable
  73. are member of the object TPKTDRVR.
  74.  
  75. Sending and receiving packets
  76. =============================
  77.  
  78. IEEE 802.3 (Ethernet) Frame
  79.  
  80. ┌──────────┬────────┬─────────┬─────────┬─────────┬─────────────────┬─────────┐
  81. │ 7 Bytes  │ 1 Byte │ 6 Bytes │ 6 Bytes │ 2 Bytes │ 46...1500 Bytes │ 4 Bytes │
  82. │ Preamble │   SFD  │ Dest.   │ Source  │ Length  │ of Data         │   FCS   │
  83. │          │        │ Address │ Address │ (Type)  │ (at least 46 B) │         │
  84. └──────────┴────────┴─────────┴─────────┴─────────┴─────────────────┴─────────┘
  85.  
  86.                     │                                               │
  87.                     └───────────────────────────────────────────────┘
  88.                      That is what you have to send.
  89.                      That is what you receive.
  90.  
  91. If you send a packet (SendPkt-Function,As_SendPkt-Function) you have to
  92. pass a buffer to the function.
  93.  
  94. BUFFER
  95. ------
  96. 0                     6                      12              14
  97. ┌─────────────────────┬──────────────────────┬───────────────┬──┤...├──────┐
  98. │ 6 Bytes dest. addr. │ 6 Bytes source addr. │ Length (Type) │ Da...ta     │
  99. └─────────────────────┴──────────────────────┴───────────────┴──┤...├──────┘
  100.  
  101. The Length of an Ethernet packet
  102. --------------------------------
  103.  
  104. The maximum length of an ethernet packet is 1518 bytes
  105.  
  106.   6 bytes (dest) + 6 bytes (src) + 2 bytes (length) + 46-1500 bytes (data) +
  107.   4 bytes (FCS) = 1518 bytes
  108.  
  109.   This is because every station must be able to detect a collision on the
  110.   network. (Defined for a network with 2500 meters; 5 segments of 500 meters
  111.   connected to each other with four repeaters)
  112.  
  113.   A packet must be at least 64 bytes which means 46 or more bytes of data.
  114.   If your data packet is smaller than 46 bytes you have to fill it up with
  115.   dummy bytes.
  116.  
  117.   The following record definition is my favourite data structure to store
  118.   packets received from packet driver.
  119.  
  120.   CONST EthernetPacketLen = 1520;
  121.  
  122.   TYPE
  123.         TPacketBuffer     = RECORD
  124.                               DataLen : Word; { Contents of register CX }
  125.                               Data    : Array[00..EthernetPacketLen-1] of Byte;
  126.                             END;
  127.  
  128. The receiver
  129. ============
  130.  
  131. When receiving frames using packet drivers you have to write a receiver 
  132. function.
  133.  
  134. For each arriving frame this function is called twice. First, the packet
  135. driver calls the function to get a pointer to a free memory block. The
  136. second call signals completion; that means the frame has been copied to your
  137. buffer.
  138.  
  139.   1. Packet driver calls your function with AX = 0.
  140.  
  141.      AX = 0
  142.      CX = Size of frame including dest. address, source address and type
  143.           field. That means size of data + 18 Bytes.
  144.  
  145.        - You have to pass back a pointer in ES:DI to the packet driver
  146.          if you want to grab the packet otherwise ES:DI must be set to
  147.          0000:0000.
  148.  
  149.          ATTENTION ! Because you are in an interrupt (IRQ of LAN Adapter)
  150.                      you can't call any DOS-Functions (Int 21h) !!!!
  151.                      So allocate memory when your program starts. An
  152.                      array as an example.
  153.  
  154.   2. Packet driver calls your function with AX = 1
  155.  
  156.      AX = 1
  157.  
  158.        - This signals completion. Packet driver has copied the frame
  159.          including the header (6+6+2 Bytes) to your buffer.
  160.  
  161. Make sure your receiver doesn't perform any timeconsuming work !
  162.  
  163. Example of a receiver
  164. ---------------------
  165.  
  166. VAR LastFrame   : Array[00..1520] of Byte; { Buffer for arriving frames           }
  167.     FrameCount  : Word;                    { Flag to signal if buffer is occupied }
  168.     FrameLength : Word;                    { Length of the last frame arrived     }
  169.  
  170. {$S-}PROCEDURE pktReceiver; ASSEMBLER;
  171. ASM
  172.   PUSH AX                      { Push registers onto stack }
  173.   PUSH BX
  174.   PUSH CX
  175.   PUSH DX
  176.  
  177.  
  178.   CMP  AX,0001                 { AX=1 means frame copied }
  179.   JZ   @@FrameCopied
  180.   CMP  AX,0000                 { AX=0 means allocate memory please }
  181.   JZ   @@AllocMemory
  182.   JMP  @@EXIT                  { Invalid register contents for AX so exit}
  183.  
  184. @@AllocMemory:
  185.  
  186.   MOV  DX,0                    { ES:DI = 0000:0000, we don't want the packet }
  187.   MOV  ES,DX
  188.   MOV  DI,0                    { We don't grab the packet }
  189.  
  190.   MOV  DX,SEG FrameCount       { Set correct data segment }
  191.   MOV  DS,DX
  192.   MOV  DX,FrameCount           { Check if buffer is free (FrameCount = 0)    }
  193.   CMP  DX,0
  194.  
  195.   JNZ  @@Exit                  { buffer is not free ! So exit with ES:DI = 0000:0000 }
  196.  
  197.   MOV  DX,SEG LastFrame        { Return a valid address for storage to the           }
  198.   MOV  ES,DX                   { packet driver. ES:DI = Seg(LasFrame):Ofs(LastFrame) }
  199.   MOV  DI,OFFSET LastFrame
  200.  
  201.   MOV  DX,SEG FrameLength
  202.   MOV  DS,DX
  203.   MOV  SI,OFFSET FrameLength
  204.   MOV  WORD PTR DS:[SI],CX     { Store length of frame in FrameLength }
  205.  
  206.   JMP  @@Exit
  207.  
  208. @@FrameCopied:
  209.  
  210.   MOV  DX,SEG FrameCount       { Set correct data segment }
  211.   MOV  DS,DX
  212.   MOV  FrameCount,1            { Set Flag to 1 }
  213.  
  214.   { Now our buffer is occupied (FrameCount = 1). Our receiver will
  215.     not accept any packets unless FrameCount = 0.
  216.   }
  217.  
  218. @@Exit:
  219.  
  220.   POP